﻿using RXY_Mall.Core;
using RXY_Mall.Core.Tool;
using RXY_Mall.Data;
using RXY_Mall.Domain;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RXY_Mall.Services.Finance
{
    public class FinanceService
    {
        private RXY_MallContext db = new RXY_MallContext();

        #region 直接购买商品红包
        //public bool AddFinanceRepacketBuy(int order_id)
        //{
        //    var flag = false;
        //    try
        //    {
        //        var order = db.gw_orders
        //            .Include(o => o.gw_user)
        //            .Include(o => o.gw_order_goods)
        //            .FirstOrDefault(o => o.id == order_id);

        //        //红包=利润的60%
        //        var redpacket = (decimal)(order.gw_order_goods.pay_price * order.gw_order_goods.quantity * order.gw_order_goods.service_rate / 100M * 0.6m);

        //        var gw_finance_repacket_buy = new gw_finance_redpacket_buy()
        //        {
        //            order_id = order.id,
        //            user_id = order.user_id,
        //            is_member = order.gw_user.grade > (int)UserGrade.Ordinary ? true : false,
        //            add_time = DateTime.Now,
        //            product_redpacket = redpacket,
        //            status = (byte)BuyRedpacket.待处理,
        //        };
        //        db.gw_finance_redpacket_buys.Add(gw_finance_repacket_buy);
        //        db.SaveChanges();
        //        flag = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.WriteLog(ex);
        //    }

        //    return flag;
        //}

        public bool SecondFinanceRepacketBuy(int order_id)
        {
            bool flag = false;
            try
            {
                var redpacket_buy = db.gw_finance_redpacket_buys.FirstOrDefault(b => b.order_id == order_id);
                if (redpacket_buy != null && redpacket_buy.status == (byte)BuyRedpacket.待处理)
                {
                    redpacket_buy.status = (byte)BuyRedpacket.领取中;
                    db.Entry(redpacket_buy).Property(b => b.status).IsModified = true;
                    //给父级发红包
                    var parentRepakcet = db.gw_finance_redpacket_buys.OrderByDescending(b => b.id).FirstOrDefault(b => b.id < redpacket_buy.id && b.status < (byte)BuyRedpacket.取消);
                    if (parentRepakcet != null)
                    {
                        parentRepakcet.child_redpacket_buy_id = redpacket_buy.id;
                        parentRepakcet.acquire_redpacket = redpacket_buy.product_redpacket;
                        db.Entry(parentRepakcet).Property(b => b.child_redpacket_buy_id).IsModified = true;
                        db.Entry(parentRepakcet).Property(b => b.acquire_redpacket).IsModified = true;
                    }
                    db.SaveChanges();
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
            }
            return flag;
        }

        public bool CancelFinanceRepacketBuy(int order_id)
        {
            bool flag = false;
            try
            {
                var redpacket_buy = db.gw_finance_redpacket_buys.FirstOrDefault(b => b.order_id == order_id);
                if (redpacket_buy != null && redpacket_buy.status == (byte)BuyRedpacket.待处理)
                {
                    redpacket_buy.status = (byte)BuyRedpacket.取消;
                    db.Entry(redpacket_buy).Property(b => b.status).IsModified = true;
                    if (redpacket_buy.child_redpacket_buy_id > 0)
                    {
                        //给父级发红包
                        var parentRepakcet = db.gw_finance_redpacket_buys.OrderByDescending(b => b.id).FirstOrDefault(b => b.id < redpacket_buy.id && b.status < (byte)BuyRedpacket.取消);
                        if (parentRepakcet != null)
                        {
                            parentRepakcet.child_redpacket_buy_id = redpacket_buy.child_redpacket_buy_id;
                            parentRepakcet.acquire_redpacket = redpacket_buy.acquire_redpacket;
                            db.Entry(parentRepakcet).Property(b => b.child_redpacket_buy_id).IsModified = true;
                            db.Entry(parentRepakcet).Property(b => b.acquire_redpacket).IsModified = true;
                        }
                    }

                    db.SaveChanges();
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
            }
            return flag;
        }
        #endregion
    }
}
